<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
  <h1>烘焙坊首页</h1>
    <!--判断是否登录,显示不同的内容-->
    <div v-if="isLogin"><!--已经登录-->
        <h3>欢迎{{user.nickname}}回来!</h3>
        <a href="/insert.html">发表食谱</a>
        <a href="javascript:void(0)" @click="logout()">退出登录</a>
    </div>
    <div v-else><!--未登录-->
        <a href="/reg.html">注册</a>
        <a href="/login.html">登录</a>
    </div>
    <hr>
    <div v-for="b in arr">
        <h3>
            <a :href="b.id">
                {{b.nickname}}:{{b.content}}:{{b.imgUrl}}
            </a>
        </h3>
    </div>
</div>
<!--引入axios和vue框架文件-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<script>
    let v = new Vue({
        el:"body>div",
        data:{
            isLogin:false,
            user:{},
            arr:[]
        },
        methods:{
            logout(){
                if (confirm("您确认退出登录吗?")){
                    axios.post("/logout").then(function (response) {

                        v.isLogin=false;
                    })
                }
            }
        },
        created:function () {
            //发请求获取当前登录的用户对象
            axios.post("/currentUser").then(function (response) {
                v.user = response.data;
                v.isLogin = !response.data=="";
            })
            //发请求获取所有烘焙坊数据
            axios.post("/selectIndex").then(function (response) {
                v.arr = response.data;
            })
        }
    })
</script>
</body>
</html>